/**
 * 逻辑参数
 */
(function(){
	var _LogicParams = function(){
	};
	
	_LogicParams.prototype.data = null; // 问卷数据
	_LogicParams.prototype.logiclist = new Array(); // 逻辑数据
	_LogicParams.prototype.QUESTION_PREFIX = "Q";
	_LogicParams.prototype.TEXT_NAME_LENGTH = 30;
	_LogicParams.prototype.LIST_NAME_LENGTH = 60;
	
	// 保存逻辑
	_LogicParams.prototype.saveLogic  = function(){
		var logiclist = LogicTool.DeepCopy(this.logiclist);
		for(var i=0;i<logiclist.length;i++){
			logiclist[i].masterQuestion = undefined;
			logiclist[i].detailQuestion = undefined;
		}
		$.ajax({
			url: ctx + "/logic/saveLogic.do",
			data: {
					surveyuuid: this.data.survey.surveyuuid,
					logiclist : $.toJSON(logiclist)
			},
			dataType: "text",
			type: "post",
			async:false,
			cache:false,
			success: function(data){
				alert(data);
			}
		});
	};
	
	// 根据问题序号获取问题
	_LogicParams.prototype.getQuestionBySortindex  = function(sortindex){
		return this.data.questionlist[sortindex];
	};
	
	// 根据问题UUID获取问题
	_LogicParams.prototype.getQuestionByUUID  = function(questionuuid){
		for(var i = 0; i < this.data.questionlist.length ; i++){
			if(this.data.questionlist[i].questionuuid == questionuuid){
				return this.data.questionlist[i];
			}
		}
	};
	
	// 根据问题序号获取问题
	_LogicParams.prototype.getAnswerByAnsweruuid  = function(answers,answeruuid){
		for(var i = 0;i < answers.length; i++ ){
			if(answers[i].answeruuid == answeruuid ){
				return answers[i];
			}
		}
	};
	
	// 获取详细逻辑问题
	_LogicParams.prototype.getDetailQuestion = function(questionuuid,type){
		for(var i = 0; i < this.data.questionlist.length ; i++){
			if(this.data.questionlist[i].questionuuid == questionuuid){
				return this.data.questionlist[i];
			}
		}
		return this.data.questionlist[0];
	}
	
	// 获取主逻辑问题
	_LogicParams.prototype.getMasterQuestion = function(questionuuid,type,logicmaster){
		if(null != logicmaster && undefined != logicmaster){
			var masterQuestion = logicmaster.masterQuestion;
			return this.data.questionlist[masterQuestion.sortindex];
		}
		for(var i = 0; i < this.data.questionlist.length ; i++){
			if(this.data.questionlist[i].questionuuid == questionuuid){
				return this.data.questionlist[i];
			}
		}
		return this.data.questionlist[1];
	}
	
	// 获取详细问题下拉
	_LogicParams.prototype.getDetailQuestionList = function(type){
		var resultlist = LogicTool.DeepCopy(this.data.questionlist);
		resultlist.remove(this.data.questionlist.length - 1);
		return resultlist;
	}
	
	// 获取详细问题下拉
	_LogicParams.prototype.getMasterQuestionList = function(logicmaster){
		var resultlist = LogicTool.DeepCopy(this.data.questionlist);
		for(var i = 0; i < logicmaster.detailQuestion.sortindex; i ++){
			resultlist.remove(0);
		}
		return resultlist;
	}
	
	// 逻辑类型
	var logicMap = {};
	logicMap["1"] = {name:"跳题逻辑",type:8};
	logicMap["2"] = {name:"前提逻辑",type:1};
	logicMap["3"] = {name:"终止逻辑",type:3};
	logicMap["4"] = {name:"分值大小比较逻辑",type:3};
	logicMap["5"] = {name:"选项随机显示逻辑",type:21};
	logicMap["6"] = {name:"选项显示控制逻辑",type:2};
	logicMap["7"] = {name:"选项互斥逻辑",type:6};
	logicMap["8"] = {name:"选项个数逻辑",type:3};
	logicMap["9"] = {name:"选项内容过滤逻辑",type:5};
	
	// 获取逻辑列表
	_LogicParams.prototype.getLogicTypeList = function(seq){
		var logicList = new Array();
		if(seq == 1){
			logicList.push(logicMap["1"]);
			logicList.push(logicMap["2"]);
			logicList.push(logicMap["3"]);
			logicList.push(logicMap["4"]);
		}else if(seq == 2){
			logicList.push(logicMap["5"]);
			logicList.push(logicMap["6"]);
			logicList.push(logicMap["7"]);
			logicList.push(logicMap["8"]);
			logicList.push(logicMap["9"]);
		}
		return logicList;
	};
	
	var codtionMap = {};
	codtionMap["1"] = {name:"选中",value:"1"};
	codtionMap["2"] = {name:"未选中",value:"2"};
	
	_LogicParams.prototype.codtionMap = codtionMap;
	
	// 获取逻辑列表
	_LogicParams.prototype.getCondtionList = function(type){
		var conditionList = new Array();
		if(type == 8){ // 前提逻辑
			conditionList.push(codtionMap["1"]);
			conditionList.push(codtionMap["2"]);
		}else{
			alert("参数有问题！");
		}
		return conditionList;
	};
	window.LogicParams = new _LogicParams();
})();